//
//  ThreeBtnViewController.m
//  Food
//
//  Created by 15 on 15/7/17.
//  Copyright (c) 2015年 李志远. All rights reserved.
//

#import "ThreeBtnViewController.h"
#import "AFNet.h"
#import "HealthKonw.h"
#import "BaseHeader.h"
#import "ThreeDetailViewController.h"
@interface ThreeBtnViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, pushDelegate, MBProgressHUDDelegate>

@end

@implementation ThreeBtnViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    Reachability *r = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    if (r.currentReachabilityStatus == NotReachable) {
        UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil];
        [alert1 show];
        [alert1 release];
        return;
    }
    [self createProgress];
    [self getData];
}

- (void)createProgress
{
    self.progress = [[MBProgressHUD alloc] initWithView:self.view];
    self.progress.labelText = @"稍等";
    self.progress.detailsLabelText = @"正在加载...";
    self.progress.delegate = self;
    [self.progress show:YES];
    [self.view addSubview:self.progress];
}

#pragma mark - 解析数据~精选
- (void)getData
{
    self.arrHttp = @[SELECT, COOKING, TALKING];
    self.arrInfo = [NSMutableArray array];
    self.arrTalking = [NSMutableArray array];
    self.arrCooking = [NSMutableArray array];
    self.arrData = [NSMutableArray array];
    [AFNet connectNetGetDataWithURLString:self.arrHttp[0] parameters:nil finishblock:^(id result) {
            self.progress.hidden = YES;
        NSDictionary *dic = result[@"data"];
        NSArray *arr = dic[@"nous"];
        for (NSDictionary *d in arr) {
            HealthKonw *health = [[[HealthKonw alloc] initWithDic:d] autorelease];
            [self.arrInfo addObject:health];
        }
        [self getDataCooking];


    }];
}
#pragma mark - 解析数据~技巧
- (void)getDataCooking
{

    [AFNet connectNetGetDataWithURLString:self.arrHttp[1] parameters:nil finishblock:^(id result) {

        Reachability *r = [Reachability reachabilityWithHostName:@"www.baidu.com"];
        if (r.currentReachabilityStatus == NotReachable) {
            UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil];
            [alert1 show];
            [alert1 release];
            return;
        }
        self.progress.hidden = YES;
        NSDictionary *dic = result[@"data"];
        NSArray *arr = dic[@"nous"];
        for (NSDictionary *d in arr) {
            HealthKonw *health = [[[HealthKonw alloc] initWithDic:d] autorelease];
            [self.arrCooking addObject:health];
        }
        [self getDataTalk];
    }];
}
#pragma mark - 解析数据~杂谈
- (void)getDataTalk
{
    [AFNet connectNetGetDataWithURLString:self.arrHttp[2] parameters:nil finishblock:^(id result) {
        Reachability *r = [Reachability reachabilityWithHostName:@"www.baidu.com"];
        if (r.currentReachabilityStatus == NotReachable) {
            UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil];
            [alert1 show];
            [alert1 release];
            return;
        }
        self.progress.hidden = YES;
        NSDictionary *dic = result[@"data"];
        NSArray *arr = dic[@"nous"];
        for (NSDictionary *d in arr) {
            HealthKonw *health = [[[HealthKonw alloc] initWithDic:d] autorelease];
            [self.arrTalking addObject:health];
        }
        [self createView];
        [self.collectionView setContentOffset:CGPointMake(320 * self.btnInt, 0) animated:NO];
    }];
}

- (void)createView
{
    [self createThreeBtn];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[[UICollectionView alloc] initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH, SCREEN_HEIGHT - 69) collectionViewLayout:layout] autorelease];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.pagingEnabled = YES;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [layout release];
    [self.collectionView registerClass:[ThreeBtnCollectionViewCell class] forCellWithReuseIdentifier:@"cell11"];
    [self.view addSubview:self.collectionView];
}

- (void)createThreeBtn
{
    NSArray *arrName = @[@"美食精选", @"烹饪技巧", @"美食杂谈"];
    self.viewThreeBtn = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)] autorelease];
    [self.view addSubview:self.viewThreeBtn];
    [self.viewThreeBtn isFirstResponder];
    self.viewThreeBtn.backgroundColor = [UIColor whiteColor];
    for (NSInteger i = 0; i < 3; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(((SCREEN_WIDTH - 2) / 3) * i + i, 2.5, (SCREEN_WIDTH - 2) / 3, 27);
        [btn setTitle:arrName[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        btn.backgroundColor = [UIColor whiteColor];
        [btn setTitleColor:FONTCOLOR forState:UIControlStateNormal];
        btn.tag = 1000 + i;
        if (i == self.btnInt) {
            btn.selected = YES;
        }
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.viewThreeBtn addSubview:btn];
    }
}
#pragma mark - webView push的代理方法
- (void)pushDelegateWith:(NSString *)idcode titleName:(NSString *)titleName img:(NSString *)img
{
    ThreeDetailViewController *viewDetail = [[[ThreeDetailViewController alloc] init] autorelease];
    viewDetail.IDCode = idcode;
    viewDetail.titleName = titleName;
    viewDetail.img = img;
    [self.navigationController pushViewController:viewDetail animated:YES];
}

- (void)btnAction:(UIButton *)btn
{
    for (UIButton *b in self.viewThreeBtn.subviews) {
        if ([b class] == [UIButton class]) {
            b.selected = NO;
        }
    }
    btn.selected = YES;
    [self.collectionView setContentOffset:CGPointMake(320 * (btn.tag - 1000), 0) animated:YES];

}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 3;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.btnInt = indexPath.row;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger i = self.collectionView.contentOffset.x / 320;

    for (UIButton *b in self.viewThreeBtn.subviews) {
        if ([b class] == [UIButton class]) {
            b.selected = NO;
        }
    }
    ((UIButton *)([self.viewThreeBtn viewWithTag: 1000 + i])).selected = YES;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(SCREEN_WIDTH - 10, SCREEN_HEIGHT - 49);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 10);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ThreeBtnCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell11" forIndexPath:indexPath];
    cell.pushDelegate = self;
    if (indexPath.item == 0) {
        cell.arrModel = self.arrInfo;
        cell.type = @"jx";
    } else if(indexPath.item == 1) {
        cell.arrModel = self.arrCooking;
        cell.type = @"jq";
    } else  if(indexPath.item == 2){
        cell.arrModel = self.arrTalking;
        cell.type = @"zt";
    }
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation

 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
